package com.vgemv.jsutilitysdkdemo.module.List.WordLive;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.vgemv.jsutilitysdkdemo.R;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;

public class WordLiveView extends JsMenuItemView {


    private RelativeLayout mrl_root;
    private TextView mtv_date;
    private TextView mtv_time;
    private ImageView miv_logo;
    private TextView mtv;

    public WordLiveView(Context context) {
        super(context);
    }


    @Override
    public void customSetVm(Context context, JsMenuItemVm vm) {
        WordLiveViewVm chatVm = (WordLiveViewVm) vm;
        //由于image要动态跟新,每次都是使用新的根视图
        {
            if(mrl_root != null) {
                removeView(mrl_root);
            }
            mrl_root = new RelativeLayout(context);
            mrl_root.setId(View.generateViewId());
            mrl_root.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));
            addView(mrl_root);
        }

        {
            if(chatVm.mb_isShowDate) {
                mtv_date = new TextView(context);
                mtv_date.setId(View.generateViewId());
                mrl_root.addView(mtv_date);

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, WordLiveViewVm.dateHeight);
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.setMargins(WordLiveViewVm.itemViewContentHPadding, WordLiveViewVm.itemViewContentTopPadding,0, 0);
                mtv_date.setLayoutParams(lp);

                mtv_date.setText(chatVm.model.publishDate);
                mtv_date.setGravity(Gravity.CENTER);
                mtv_date.setTextColor(Color.argb(255,160,160,160));
                mtv_date.setBackgroundColor(Color.argb(255,255,255,255));
            }
        }
        {
            mtv_time = new TextView(context);
            mtv_time.setId(View.generateViewId());
            mrl_root.addView(mtv_time);

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, WordLiveViewVm.timeHeight);
            if(chatVm.mb_isShowDate) {
                lp.addRule(RelativeLayout.BELOW,mtv_date.getId());
            }
            else {
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            }
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lp.setMargins(WordLiveViewVm.itemViewContentHPadding, WordLiveViewVm.itemViewContentTopPadding,0, 0);
            mtv_time.setLayoutParams(lp);

            mtv_time.setText(chatVm.model.publishTime);
            mtv_time.setGravity(Gravity.CENTER);
            mtv_time.setTextColor(Color.argb(255,200,0,0));
            mtv_time.setBackgroundColor(Color.argb(255,255,255,255));

        }
        {
            miv_logo = new ImageView(context);
            miv_logo.setId(View.generateViewId());
            mrl_root.addView(miv_logo);

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(300, 80);
            if(chatVm.mb_isShowDate) {
                lp.addRule(RelativeLayout.BELOW,mtv_date.getId());
            }
            else {
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            }
            lp.addRule(RelativeLayout.RIGHT_OF,mtv_time.getId());
            lp.setMargins(WordLiveViewVm.itemViewContentHPadding, WordLiveViewVm.itemViewContentTopPadding,0, 0);
            miv_logo.setLayoutParams(lp);

            miv_logo.setImageResource(R.mipmap.live_logo);
        }

        {
//            mtv
                mtv = new TextView(context);
                mtv.setId(View.generateViewId());
                mrl_root.addView(mtv);

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.BELOW,mtv_time.getId());
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.setMargins(WordLiveViewVm.itemViewContentHPadding, WordLiveViewVm.itemViewContentTopPadding,0, WordLiveViewVm.itemViewContentImageVMargin);
                mtv.setLayoutParams(lp);

            mtv.setText(chatVm.model.content);
            mtv.setTextColor(Color.argb(255,0,0,0));
            mtv.setBackgroundColor(Color.argb(255,255,255,255));
        }
        {
//            imageList
            int imageCount = 0;
            if(chatVm.model.materialPicUrl != null) {
                imageCount = chatVm.model.materialPicUrl.size();
            }
            if(imageCount > 0) {
                if(imageCount == 1) {
                    //1 张图片,占满宽度,然后高度为9/16;
                    ImageView iv = new ImageView(context);
                    iv.setId(View.generateViewId());
                    //保持宽高比例,拉伸
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    iv.setBackgroundColor(Color.argb(255,200,100,100));
                    int width = JsGeometryManager.getScreenSizeInPixel(context).getWidth() - WordLiveViewVm.itemViewParentHPadding * 2 - WordLiveViewVm.itemViewContentHPadding * 2;
                    int height = width * 9 / 16;
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width,height);
                    lp.addRule(RelativeLayout.BELOW,mtv.getId());
                    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    lp.setMargins(WordLiveViewVm.itemViewContentHPadding,0,0, WordLiveViewVm.imageBottomPadding);
                    mrl_root.addView(iv);
                    iv.setLayoutParams(lp);

                    //内容
                    String imageUrlString = chatVm.model.materialPicUrl.get(0);
                    Picasso.get().load(imageUrlString)
                            .placeholder(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                            .error(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                            .into(iv);
                }
                else {
                    //计算图片宽度
                    int width = (JsGeometryManager.getScreenSizeInPixel(context).getWidth() - WordLiveViewVm.itemViewParentHPadding * 2 - WordLiveViewVm.itemViewContentHPadding * 2 - WordLiveViewVm.imageImageSpacing * 2)/3;
                    int height = width;

                    //计算图片的行数  2,3 ==> 1; 4,5,6 ==> 2;
                    int rowCount = (imageCount - 1)/3 + 1;

                    for (int r = 0 ; r < rowCount ; r ++) {
                        for (int c = 0; c < 3; c++) {
                            int i = (r * 3) + c;
                            if(i >= imageCount) {
                                break;
                            }
                            //1 张图片,占满宽度,然后高度为9/16;
                            ImageView iv = new ImageView(context);
                            iv.setId(View.generateViewId());
                            //保持宽高比例,拉伸
                            iv.setScaleType(ImageView.ScaleType.FIT_XY);

                            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
                            lp.addRule(RelativeLayout.BELOW, mtv.getId());
                            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                            int xOffset = WordLiveViewVm.itemViewContentHPadding + width * c + WordLiveViewVm.imageImageSpacing * c;
                            int yOffset = height * r + WordLiveViewVm.imageImageSpacing * r;
                            int bottom = i == imageCount - 1 ? WordLiveViewVm.imageBottomPadding : 0;
                            lp.setMargins(xOffset, yOffset, 0,bottom);
                            mrl_root.addView(iv);
                            iv.setLayoutParams(lp);

                            //内容

                            String imageUrlString = chatVm.model.materialPicUrl.get(i);
                            Picasso.get().load(imageUrlString)
                                    .placeholder(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                                    .error(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                                    .into(iv);

                        }
                    }
                }
            }
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
            Paint paint = new Paint();
            paint.setColor(Color.argb(255,200,0,0));
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);

            Path path = new Path();
            WordLiveViewVm vm = (WordLiveViewVm)mvm;
            int y = 0;
            if(vm.mb_isShowDate) {
                y = WordLiveViewVm.itemViewContentTopPadding * 2 + WordLiveViewVm.dateHeight + WordLiveViewVm.timeHeight/2;
            }
            else {
                y = WordLiveViewVm.itemViewContentTopPadding + WordLiveViewVm.timeHeight/2;
            }
            int length = 15;
            path.moveTo(0,y);
            path.lineTo(length,y);
            path.addCircle(length*2,y,length,Path.Direction.CW);
            canvas.drawPath(path,paint);

    }

}
